<template>
  <div class="app-container">
    <!---查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-row>
        <el-col :span="4">
          <el-form-item label="昵称:">
            <el-input clearable v-model="queryParams.nickName" placeholder="请输入昵称" size="mini" />
          </el-form-item>
        </el-col>

        <el-col :span="4">
          <el-form-item label="用户名:">
            <el-input clearable v-model="queryParams.userName" placeholder="请输入用户名" size="mini" />
          </el-form-item>
        </el-col>

        <el-col :span="4">
          <el-form-item label="手机号:">
            <el-input type="number" clearable v-model="queryParams.mobile" placeholder="请输入手机号" size="mini" />
          </el-form-item>
        </el-col>
      </el-row>
      <div>
        <el-button type="primary" size="small" icon="el-icon-search" @click="addData()">新增</el-button>
        <el-button type="primary" size="small" icon="el-icon-download" @click="edit()">编辑</el-button>
        <el-button type="primary" size="small" icon="el-icon-search" @click="getList()">查询</el-button>
        <el-button type="default" size="small" icon="el-icon-delete" @click="resetData()">清空</el-button>
      </div>

    </el-form>
    <!--列表显示-->
    <el-table style="margin-top: 20px;" v-loading="loading" :data="list" element-loading-text="数据加载中" border fit
      highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (current - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="nickName" label="昵称" align="center" />
      <el-table-column prop="userName" label="用户名" align="center" />
      <el-table-column prop="sex" label="性别" align="center" />
      <el-table-column prop="mobile" label="手机号码" align="center" />
      <el-table-column prop="age" label="年龄" align="center" />
      <el-table-column prop="isDisabled" label="状态" align="center">
        <template slot-scope="scope">
          <el-switch v-model="!scope.row.isDisabled" disabled active-color="#13ce66" inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="添加时间" align="center" />
      <el-table-column label="操作" align="center" width="300">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="getById(scope.row.id)">详情</el-button>
          <el-button style="margin-left: 10px;" type="success" v-if="scope.row.isDisabled" size="mini"
            icon="el-icon-delete" @click="invalidOrRecovery(scope.row.id,'解禁')">解禁</el-button>
          <el-button style="margin-left: 10px;" type="warning" v-else size="mini" icon="el-icon-delete"
            @click="invalidOrRecovery(scope.row.id,'禁用')">禁用</el-button>
          <el-button style="margin-left: 10px;" type="danger" size="mini" icon="el-icon-delete"
            @click="deleteById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination :current-page="current" :page-size="limit" :total="total"
      style="padding: 30px 0; text-align: center;" background layout="total, prev, pager, next, jumper"
      @current-change="getList" />

    <!--详情-->
    <el-dialog :title="editTitle" :visible.sync="openEditForm">
      <el-form :model="form" label-width="100px">
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" :disabled="isEdit"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" :disabled="isEdit"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="form.sex" disabled size="mini" :disabled="isEdit">
            <el-radio-button label="男"></el-radio-button>
            <el-radio-button label="女"></el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" :disabled="isEdit" style="width:180px;"> </el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="form.mobile" :disabled="isEdit"> </el-input>
        </el-form-item>

        <el-form-item label="车牌号" prop="plateNum">
          <el-input v-model="form.plateNum" :disabled="isEdit"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <template>
            <el-switch v-model="!form.isDisabled" :disabled="isEdit" active-color="#13ce66" inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-form-item>
        <el-form-item label="加入时间" prop="createTime">
          <el-input v-model="form.createTime" :disabled="isEdit"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancleEditForm()">关 闭</el-button>

      </div>
    </el-dialog>

    <!--新增-->
    <el-dialog :title="addTitle" ref="form" :visible.sync="openAddForm" @close="cancleAddForm" width="600px">
      <el-form :model="form" label-width="100px" ref="form" :rules="rules">
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" :disabled="isEdit" style="width:240px;"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" :disabled="isEdit" style="width:240px;"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" :disabled="isEdit" style="width:240px;"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="form.sex" disabled size="mini" :disabled="isEdit">
            <el-radio-button label="男"></el-radio-button>
            <el-radio-button label="女"></el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" :disabled="isEdit" style="width:180px;"> </el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="form.mobile" :disabled="isEdit" style="width:180px;"> </el-input>
        </el-form-item>

        <el-form-item label="车牌号" prop="plateNum">
          <el-input v-model="form.plateNum" :disabled="isEdit" style="width:180px;"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <template>
            <el-switch v-model="!form.isDisabled" disabled active-color="#13ce66" inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-form-item>
        <el-form-item label="加入时间" prop="createTime">
          <el-input v-model="form.createTime" disabled style="width:240px;"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancleAddForm(form)">取 消</el-button>
        <el-button type="primary" @click="addSubmit(form)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import user from '@/api/user'


  export default {
    components: {

    },
    data () {
      var user_name = /^[a-zA-Z0-9]{6,14}$/; //用户名
      var hiPass = /^.*(?=.{6,16})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/;
      var sex = /^[男|女]{1}$/;//性别
      var reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;//手机号码
      var cPattern = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;//车牌号

      //用户名校验
      var userNameCheck = (rule, value, callback) => {
        if (value != null) {
          if (!user_name.test(value)) {
            callback(new Error('用户名是由6到14位的字母数字组合字符串！'));
          } else {
            callback();
          }
        } else {
          callback(new Error('请输入用户名！'));
        }
      };
      //密码校验
      var passCheck = (rule, value, callback) => {
        if (value != null) {
          if (!hiPass.test(value)) {
            callback(new Error('必须是由6到16位的包括大小写字母、数字、特殊字符，组成的字符串'));
          } else {
            callback();
          }
        } else {
          callback(new Error('请输入密码！'));
        }
      };
      //手机号码校验
      var mobileCheck = (rule, value, callback) => {
        if (value != null) {
          if (!reg_tel.test(value)) {
            callback(new Error('手机号码由11位数字组成！'));
          } else {
            callback();
          }
        } else {
          callback(new Error('请输入手机号！'));
        }
      };
      //性别校验
      var sexCheck = (rule, value, callback) => {
        if (value != null) {
          if (!sex.test(value)) {
            callback(new Error('性别信息输入有误！'));
          } else {
            callback();
          }
        } else {
          callback(new Error('请输入性别！'));
        }
      };
      //车牌校验
      var plateNumCheck = (rule, value, callback) => {
        if (value != null) {
          if (!cPattern.test(value)) {
            callback(new Error('车牌信息输入有误！'));
          } else {
            callback();
          }
        } else {
          callback(new Error('请输入车牌号码！'));
        }
      };

      return {
        current: 1,//页码
        limit: 10,//每页多少条
        queryParams: {},//查询条件
        list: [],//列表数据
        total: 0,//总记录数
        loading: true,//提示loading信息
        multipleSelection: null,//选中数据
        selectId: null,//选中id
        isEdit: true,//是否禁用

        editTitle: null,
        addTitle: null,

        openEditForm: false,
        openAddForm: false,

        //form表单
        form: {
          nickName: null,
          mobile: null,
          userName: null,
          password: null,
          sex: null,
          age: null,
          plateNum: null,
          isDisabled: null,
          createTime: null,
        },

        queryData: {
          pageNo: null,
          pageSize: null,
          params: null,
        },//查询参数

        rules: {
          nickName: [
            { required: true, message: '昵称不能为空', trigger: 'blur' },
          ],
          userName: [
            { validator: userNameCheck, trigger: 'blur' },
          ],
          password: [
            { validator: passCheck, trigger: 'blur' },
          ],
          sex: [
            { validator: sexCheck, trigger: 'blur' },

          ],
          mobile: [
            { validator: mobileCheck, trigger: 'blur' },
          ],
          plateNum: [
            { validator: plateNumCheck, trigger: 'blur' },
          ],
        },


      }
    },
    created () {
      this.getList();
    },
    methods: {
      handleSelectionChange (row) {
        this.selectId = null;
        this.multipleSelection = row;
        for (var tmp of this.multipleSelection) {
          this.selectId = tmp.id;
        }
      },
      getList () {
        this.loading = true;
        this.queryData.pageNo = this.current;
        this.queryData.pageSize = this.limit;
        this.queryData.params = this.queryParams;
        user.getAllUserByPage(this.queryData)
          .then(res => {
            this.list = res.data.userList;
            this.total = res.data.total;
            this.loading = false;
          })

      },
      resetData () {
        this.queryParams = {};
        this.getList();
      },
      //新增
      addData () {
        this.form = {};
        this.addTitle = "新增管理员"
        this.openAddForm = true;
        this.isEdit = false;
      },
      //新增提交
      addSubmit (form) {
        if (form.id != null && form.id != "") {
          //修改
          user.addOrEdit(form).then(res => {
            this.$message({ type: 'success', message: '操作成功!' });
            this.getList();
          })
        } else {
          this.$refs.form.validate((valid, obj) => {
            if (valid) {
              user.addOrEdit(form).then(res => {
                this.$message({ type: 'success', message: '操作成功!' });
                this.getList();
              })
            } else {
              return false;
            }
          });
        }
      },
      //新增取消
      cancleAddForm (form) {
        this.openAddForm = false;
        this.$refs.form.resetFields();
      },
      //编辑
      edit () {
        if (this.selectId != null && this.selectId != "") {
          var id = this.selectId
          user.getUserById(id).then(res => {
            this.form = res.data.vo;
            this.addTitle = "编辑用户"
            this.openAddForm = true;
            this.isEdit = false;
          })
        } else {
          this.$message({
            type: 'warning',
            message: '请选择要编辑的数据行!'
          })
        }
      },
      //详情
      getById (id) {
        user.getUserById(id).then(res => {
          this.form = res.data.vo;
          this.editTitle = "用户详情"
          this.openEditForm = true;
          this.isEdit = true;
        })
      },
      cancleEditForm () {
        this.openEditForm = false;
      },
      //禁用 解禁
      invalidOrRecovery (id, type) {
        console.log(id, type);
        if (type === '解禁') {
          this.$confirm('此操作将该用户解除禁用, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            user.invalidOrRecovery(id).then(res => {
              this.$message({
                type: 'success',
                message: '解除禁用成功!'
              });
              this.getList();
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消解除禁用操作'
            });
          });
        } else if (type === '禁用') {
          this.$confirm('此操作将该用户禁用, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            user.invalidOrRecovery(id).then(res => {
              this.$message({
                type: 'success',
                message: '禁用成功!'
              });
              this.getList();
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消禁用操作'
            });
          });
        }
      },


      deleteById (id) {
        this.$confirm('此操作将删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          user.deleteById(id).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList();
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },



    }

  }
</script>

<style>
</style>